﻿namespace ExamV5.WorkExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmManagRubricInfo : Page
    {
        protected Button butAdd;
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLQuery;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        protected Label labRescordCount;
        protected LinkButton LBDel;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBNotUse;
        protected LinkButton LBPrevPage;
        protected LinkButton LBSelect;
        protected LinkButton LBUse;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtQuery;
        protected UpdatePanel UPPart;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            else
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[1].Text.Length > 7)
                {
                    e.Row.Cells[1].Text = "<a title=" + e.Row.Cells[1].Text + ">" + e.Row.Cells[1].Text.Substring(0, 7) + "…</a>";
                }
                e.Row.Cells[2].Text = this.publicFun_0.RemoveHTML(e.Row.Cells[2].Text).Replace("&lt;BR&gt;", "").Replace("&lt;br&gt;", "").Replace("&nbsp;", "");
                if (e.Row.Cells[2].Text.Length > (Convert.ToInt32(this.ViewState["SubStrLen"].ToString()) + 15))
                {
                    e.Row.Cells[2].Text = "<a title=" + e.Row.Cells[2].Text + ">" + e.Row.Cells[2].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString()) + 15) + "…</a>";
                }
                Label label = (Label) e.Row.Cells[4].FindControl("labEdit");
                Label label2 = (Label) e.Row.Cells[4].FindControl("labLook");
                label.Text = "<a href='#' onclick=javascript:EditRubric('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');>" + label.Text + "</a>";
                label2.Text = "<a href='#' onclick=javascript:ShowRubric('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "');>" + label2.Text + "</a>";
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                string str;
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    str = this.ViewState["SortFiled"].ToString();
                    if (str != null)
                    {
                        if (str == "SubjectName")
                        {
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                        if (str == "RubricTitle")
                        {
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                        if (str == "IsDel")
                        {
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                    return;
                }
                str = this.ViewState["SortFiled"].ToString();
                switch (str)
                {
                    case null:
                        goto Label_04C7;

                    case "SubjectName":
                        e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                        return;
                }
                if (!(str == "RubricTitle"))
                {
                    if (!(str == "IsDel"))
                    {
                        goto Label_04C7;
                    }
                    e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                }
                else
                {
                    e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                }
            }
            return;
        Label_04C7:
            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (this.ViewState["SortFiled"].ToString() == sortExpression)
            {
                if (this.ViewState["SortType"].ToString() == " Desc")
                {
                    this.ViewState["SortType"] = " ASC";
                }
                else
                {
                    this.ViewState["SortType"] = " Desc";
                }
            }
            else
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            this.method_0();
        }

        protected void LBDel_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int num = 0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    if (!(this.publicFun_0.GetValues("select PaperID from WorkPaperData where '#'+RubricS+RubricD like '%#" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + "#%' and  PaperID in(select PaperID from WorkExamUser where Flag<>'A')", "PaperID") == ""))
                    {
                        flag = true;
                    }
                    else
                    {
                        command = new SqlCommand("delete  from WorkRubric where ID='" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + "';insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除职业测评试题库信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                        command.ExecuteNonQuery();
                        command.Dispose();
                    }
                }
            }
            connection.Close();
            connection.Dispose();
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
            if (flag)
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPowerAA", "alert('因部分试题已被试卷使用并有考生作答记录所以部分试题无法删除！')", true);
            }
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBNotUse_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    command = new SqlCommand("Update WorkRubric set IsDel='Y' where ID='" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + "';insert into SysLogInfo(LogDisp,EditUserCode,EditUser) values('禁用职业测评试题库信息！','" + this.httpCookie_0.Value + "','" + this.httpCookie_1.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        protected void LBSelect_Click(object sender, EventArgs e)
        {
            int num;
            CheckBox box;
            if (this.LBSelect.Text != "选中当页")
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    box.Checked = false;
                }
                this.LBSelect.Text = "选中当页";
            }
            else
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    if (box.Enabled)
                    {
                        box.Checked = true;
                    }
                }
                this.LBSelect.Text = "取消所选";
            }
        }

        protected void LBUse_Click(object sender, EventArgs e)
        {
            int num = 0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    command = new SqlCommand("update WorkRubric set IsDel='N'  where ID='" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + "';insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('启用职业测评试题库信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            connection.Dispose();
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        private void method_0()
        {
            string selectCommandText = "";
            if (this.ViewState["ManagPower"].ToString() != "Y")
            {
                if (this.ViewState["AddPower"].ToString() == "Y")
                {
                    selectCommandText = "select IsDel=case a.IsDel when 'N' then '否' when 'Y' then '是' end,a.OptionAnswer,a.OKAnswer,a.CreatNumberNO,a.ID,b.SubjectName,b.EditUserCode,a.RubricType,a.RubricTitle";
                    selectCommandText = selectCommandText + " from WorkRubric a,SubjectInfo b where a.SubjectID=b.ID and a.CreatNumberNO='" + this.httpCookie_0.Value + "'";
                }
                else
                {
                    selectCommandText = "select IsDel=case a.IsDel when 'N' then '否' when 'Y' then '是' end,a.OptionAnswer,a.OKAnswer,a.CreatNumberNO,a.ID,b.SubjectName,b.EditUserCode,a.RubricType,a.RubricTitle";
                    selectCommandText = selectCommandText + " from WorkRubric a,SubjectInfo b where a.SubjectID=b.ID and a.ID=0";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('添加试题权限未开放！')", true);
                }
            }
            else
            {
                selectCommandText = "select IsDel=case a.IsDel when 'N' then '否' when 'Y' then '是' end,a.OptionAnswer,a.OKAnswer,a.CreatNumberNO,a.ID,b.SubjectName,b.EditUserCode,a.RubricType,a.RubricTitle";
                selectCommandText = selectCommandText + " from WorkRubric a,SubjectInfo b where a.SubjectID=b.ID ";
            }
            if (this.int_0 != 0)
            {
                selectCommandText = selectCommandText + " and a.SubjectID=" + this.int_0;
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if (!((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
            else
            {
                this.int_0 = Convert.ToInt32(base.Request["SubjectID"].ToString());
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) != 0x3a)
                {
                    if (!base.IsPostBack)
                    {
                        if (this.publicFun_0.ReturnPaperData() != "Yes")
                        {
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                        }
                        else
                        {
                            this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                            connection.Open();
                            SqlCommand command = new SqlCommand("select AddRubric,ManagRubric from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                            if (reader.Read())
                            {
                                this.ViewState["AddPower"] = reader["AddRubric"].ToString();
                                this.ViewState["ManagPower"] = reader["ManagRubric"].ToString();
                            }
                            reader.Close();
                            command.Dispose();
                            connection.Close();
                            connection.Dispose();
                            this.ViewState["SortFiled"] = "ID";
                            this.ViewState["SortType"] = " Desc";
                            this.LBDel.Attributes.Add("onclick", "javascript:{if(confirm('删除试题后可能会影响已考试卷信息，确定删除所有选中信息吗？')==false) return false;}");
                            this.LBUse.Attributes.Add("onclick", "javascript:{if(confirm('确定启用所有选中信息吗？')==false) return false;}");
                            this.LBNotUse.Attributes.Add("onclick", "javascript:{if(confirm('确定禁用所有选中信息吗？')==false) return false;}");
                            string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                            if (str.Trim() == "")
                            {
                                str = "20";
                            }
                            this.ViewState["SubStrLen"] = str;
                            this.method_0();
                        }
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTHB", "alert('该版本为特惠版，此功能未开放！')", true);
                }
            }
        }
    }
}

